<template>
  <a-modal
    :visible="true"
    class="form-modal"
    fullscreen
    :footer="false"
    :closable="false"
    :esc-to-close="false"
    title-align="start"
    :modal-style="{ padding: 0, backgroundColor: '#f5f7fa' }"
    :body-style="{ padding: 0 }"
    unmount-on-close
  >
    <a-row class="head">
      <a-col :span="3">
        <p
          class="head-1"
          style="margin-left: 20px; font-size: 16px; padding: 0"
        >
          报工工作台
        </p>
      </a-col>
      <a-col :span="16">
        <div class="search">
          <a-select
            v-model="taskId"
            allow-search
            placeholder="请扫一扫任务单编码"
            style="width: 300px; margin-right: 20px"
            :options="taskData.data"
            @change="changeTaskId"
            :field-names="{ value: 'id', label: 'code' }"
          >
            <template #arrow-icon>
              <span><icon-scan :size="20" /></span>
            </template>
          </a-select>
          <a-select
            placeholder="请选择工序"
            style="width: 300px; margin-right: 20px"
            v-model="proccessId"
            :field-names="{ value: 'ProcessGroupId', label: 'name' }"
            :options="proccessList.data"
            @change="changeProccessId"
          >
          </a-select>
        </div>
      </a-col>
      <a-col :span="3">
        <div class="right">
          <a-button @click="visible = true" type="outline" style="color: #fff"
            >E-SOP</a-button
          >
          <icon-fullscreen
            v-if="!big"
            style="cursor: pointer"
            :size="20"
            @click="fullScreen"
          />
          <icon-fullscreen-exit
            style="cursor: pointer"
            @click="exit"
            v-else
            :size="20"
          />
          <icon-export @click="close" style="cursor: pointer" :size="20" />
        </div>
      </a-col>
    </a-row>
    <a-row class="main">
      <a-col :span="18">
        <div style="background-color: #fff; padding: 10px; border-radius: 10px">
          <p
            style="
              padding-bottom: 5px;
              margin-bottom: 10px;
              border-bottom: 1px solid #f5f7fa;
            "
            >生产信息
          </p>
          <div class="ProductionInformation">
            <a-row
              :span="24"
              style="overflow-y: auto; height: 100%"
              v-if="produceInfo.data?.length"
            >
              <a-col
                :span="11"
                v-for="(ele, index) in produceInfo.data"
                :key="ele.index"
                style="margin: 10px"
              >
                <div
                  class="Information"
                  :class="{ isCheck: ele.check }"
                  @click="select(index, ele.id)"
                >
                  <ProductionInformation :data="ele" />
                  <div v-if="ele.check" class="yes">
                    <icon-check class="icon" />
                  </div>
                </div>
              </a-col>
            </a-row>
            <a-empty style="height: 100%" v-else />
          </div>
          <div class="statistics">
            <div class="item" style="background-color: #2cad70">
              <div class="icon" style="color: #2cad70">
                <icon-sun :size="25" />
              </div>
              <div>
                <p>目标数量</p>
                <p>{{
                  statistics.data.targetNumber
                    ? statistics.data.targetNumber
                    : '暂无'
                }}</p>
              </div>
            </div>
            <div class="item" style="background-color: #3d7eff">
              <div class="icon" style="color: #3d7eff">
                <icon-codepen :size="25" />
              </div>
              <div>
                <p>良品数</p>
                <p>{{
                  statistics.data.goodProductNumber
                    ? statistics.data.goodProductNumber
                    : 0
                }}</p>
              </div>
            </div>
            <div class="item" style="background-color: #ff8000">
              <div class="icon" style="color: #ff8000">
                <icon-paste :size="25" />
              </div>
              <div>
                <p>不良品数</p>
                <p>{{
                  statistics.data.badProductNumber
                    ? statistics.data.badProductNumber
                    : 0
                }}</p>
              </div>
            </div>
            <div class="item" style="background-color: #5645ff">
              <div class="icon" style="color: #5645ff">
                <icon-oblique-line :size="25" />
              </div>
              <div>
                <p>不良率</p>
                <p>{{
                  statistics.data.badProductRate
                    ? statistics.data.badProductRate
                    : 0
                }}</p>
              </div>
            </div>
          </div>
        </div>
        <div
          style="
            margin-top: 10px;
            background-color: #fff;
            padding: 10px;
            border-radius: 10px;
          "
        >
          <p
            style="
              padding-bottom: 5px;
              margin-bottom: 10px;
              border-bottom: 1px solid #f5f7fa;
            "
            >报工记录
          </p>
          <a-table
            :bordered="false"
            :pagination="false"
            :columns="[
              {
                title: '派工编码',
                dataIndex: 'code',
                width: 100,
                align: 'center',
              },
              {
                title: '良品数量',
                dataIndex: 'goodProductNumber',
                width: 100,
                align: 'center',
              },
              {
                title: '不良品数量',
                dataIndex: 'badProductNumber',
                width: 100,
                align: 'center',
              },
              {
                title: '作业人员',
                dataIndex: 'operator',
                width: 100,
                align: 'center',
              },
              {
                title: '报工时间',
                dataIndex: 'reportWorkTime',
                width: 100,
                align: 'center',
                slotName: 'time',
              },
            ]"
            :data="reportWorks.data"
          >
            <template #time="{ record }">
              {{ dayjs(record.createdAt).format('YYYY-MM-DD hh:mm:ss') }}
            </template>
          </a-table>
        </div>
      </a-col>
      <a-col :span="6">
        <div style="padding: 0 10px">
          <ReportWoek
            @update="update"
            :dispatchingId="dispatchingId"
            :del-id="delId"
            :taskId="taskId"
            :sum="sum"
            :processGroupId="proccessId"
          />
        </div>
      </a-col>
    </a-row>
  </a-modal>
  <a-drawer
    title="E-SOP浏览"
    :footer="false"
    :visible="visible"
    :width="550"
    @cancel="visible = false"
    unmountOnClose
  >
    <a-carousel
      :style="{
        width: '100%',
        height: '500px',
      }"
      v-if="images.data.length"
    >
      <a-carousel-item v-for="image in images.data" :key="image">
        <img
          :src="image.fileRemark"
          :style="{
            width: '100%',
            height: '500px',
          }"
        />
      </a-carousel-item>
    </a-carousel>
    <a-empty style="height: 100%" v-else />
  </a-drawer>
</template>

<script setup lang="ts">
  import dayjs from 'dayjs';
  import { reactive, ref, onMounted, computed, watch } from 'vue';
  import {
    GetTaskPage,
    GetTaskProccessList,
    GetDispatchings,
    GetReportWorks,
    GetReportWorkStatistics,
    GetSop,
  } from '@/api/produce-management/produce-list/index';
  import { Message } from '@arco-design/web-vue';
  import ProductionInformation from './production-information/index.vue';
  import ReportWoek from './report-work/index.vue';

  const visible = ref(false);
  const dispatchingId = ref();
  const images: any = reactive({ data: [] });
  // [
  //   ('https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/cd7a1aaea8e1c5e3d26fe2591e561798.png~tplv-uwbnlip3yd-webp.webp',
  //   'https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/6480dbc69be1b5de95010289787d64f1.png~tplv-uwbnlip3yd-webp.webp',
  //   'https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/0265a04fddbd77a19602a15d9d55d797.png~tplv-uwbnlip3yd-webp.webp')
  // ];
  const big = ref(false);
  const fullScreen = () => {
    let element: any = document.documentElement;
    if (element.requestFullscreen) {
      element.requestFullscreen();
    } else if (element.mozRequestFullScreen) {
      // Firefox
      element.mozRequestFullScreen();
    } else if (element.webkitRequestFullscreen) {
      // Chrome, Safari, and Opera
      element.webkitRequestFullscreen();
    } else if (element.msRequestFullscreen) {
      // Internet Explorer and Edge
      element.msRequestFullscreen();
    }
    big.value = true;
  };
  const exit = () => {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    } else if (document.mozCancelFullScreen) {
      // Firefox
      document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
      // Chrome, Safari and Opera
      document.webkitExitFullscreen();
    } else if (document.msExitFullscreen) {
      // IE/Edge
      document.msExitFullscreen();
    }
  };
  const statistics: any = reactive({ data: {} });
  const produceInfo: any = reactive({
    data: [],
  });

  const reportWorks: any = reactive({ data: [] });
  const delId = ref();
  const sum = ref(0);
  const close = () => {
    window.close();
  };
  const select = async (index: number, id: number) => {
    try {
      if (produceInfo.data[index].check) return;
      produceInfo.data.forEach((ele: any) => {
        ele.check = false;
      });
      dispatchingId.value = id;
      const res = await GetReportWorks({ dispatchingId: id });
      const res1 = await GetReportWorkStatistics({ dispatchingId: id });
      statistics.data = res1;
      reportWorks.data = res;
      delId.value = reportWorks.data[reportWorks.data.length - 1]?.id;
      produceInfo.data[index].check = true;
      sum.value = produceInfo.data[index].schedulingNumber;
    } catch (error) {
      console.log(error);
      Message.error(`${error}`);
    }
  };
  const taskId = ref();
  const proccessId = ref();
  const keyWord = ref('');
  const taskData = reactive({ data: [] });
  const proccessList = reactive({ data: [] });
  // 获取表格数据
  const getData = async (pageData?: any) => {
    try {
      const res = await GetTaskPage({
        page: 10,
        size: -1,
        code: keyWord.value,
      });
      taskData.data = res.content;
      return Promise.resolve(res);
    } catch (error: unknown) {
      console.log(error);
    }
  };
  getData();
  const changeTaskId = async (e: any) => {
    try {
      const res = await GetTaskProccessList({ taskId: e });
      proccessList.data = res;
      produceInfo.data = [];
      images.data = [];
      reportWorks.data = [];
      statistics.data = [];
      proccessId.value = null;
      delId.value = null;
      sum.value = 0;
      dispatchingId.value = null;
    } catch (error) {
      console.log(error);
      Message.error(`${error}`);
    }
  };
  const changeProccessId = async (e: any) => {
    try {
      // taskId: taskId.value,
      const res = await GetDispatchings({ processId: e });

      proccessId.value = e;
      produceInfo.data = res;
      reportWorks.data = [];
      statistics.data = [];
      dispatchingId.value = null;
      delId.value = null;
      sum.value = 0;
      try {
        const res1 = await GetSop({ processId: e });
        images.data = res1;
      } catch (error) {
        console.log(error);
      }
    } catch (error) {
      console.log(error);
      Message.error(`${error}`);
    }
  };
  const update = async () => {
    const res = await GetReportWorks({ dispatchingId: dispatchingId.value });
    const res1 = await GetReportWorkStatistics({
      dispatchingId: dispatchingId.value,
    });
    statistics.data = res1;
    reportWorks.data = res;
  };
</script>

<style lang="less" scoped>
  p {
    margin: 0;
    padding: 0;
  }

  .head {
    background-color: #3d7eff;
    color: #fff;
    font-weight: 600;
    height: 50px;
    .arco-col {
      height: 100%;
    }
    .head-1 {
      display: flex;
      align-items: center;
      height: 100%;
    }
    .search {
      display: flex;
      height: 100%;
      align-items: center;
    }
    .right {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 100%;
      padding: 20px;
    }
  }
  .main {
    width: 100%;
    height: 100%;
    padding: 10px;
    .ProductionInformation {
      height: 250px;
      overflow: hidden;
      .Information {
        padding: 0 20px;
        cursor: pointer;
        overflow: auto;
        height: 100%;
        border: 1px solid #c4c4c4;
        border-radius: 5px;
      }
    }
    .statistics {
      display: flex;
      justify-content: space-around;
      margin-top: 20px;
      width: 100%;
      overflow: hidden;
      .item {
        border-radius: 5px;
        padding: 20px;
        flex: 1;
        margin: 0 10px;
        display: flex;
        align-items: center;
        justify-content: start;
        color: #fff;
        .icon {
          background-color: rgb(255, 255, 255);
          margin-right: 20px;
          border-radius: 50%;
          width: 40px;
          display: flex;
          height: 40px;
          align-items: center;
          justify-content: center;
        }
      }
    }
  }
  .isCheck {
    border: 1px solid #096dd9 !important;
    position: relative;
    .yes {
      text-align: center;
      background-color: #096dd9;
      position: absolute;
      bottom: 0;
      right: 0;
      width: 25px;
      height: 25px;
      // border-left: 30px solid transparent; /* 左边边框透明 */
      // border-bottom: 30px solid #096dd9; /* 底边框为红色，可以更改颜色 */
      color: #fff;
    }
  }
</style>
